package com.xdf.xzymanagementsystem.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

@Component
public class MailUtils {
    @Value("${spring.mail.username}")
    private String from;
    @Autowired
    private JavaMailSender mailSender;
    SimpleMailMessage message = new SimpleMailMessage();

    @Async("ccAlarmThreadPool")
    public void sendSimpleMail(String to,String subject,String content){
        message = new SimpleMailMessage();
        message.setTo(to);//收信人
        message.setSubject(subject);//主题
        message.setText(content);//内容
        message.setFrom(from);//发信人

        try {
            mailSender.send(message);
        }catch (Exception e){
            System.out.println(e);
        }
    }

    @Async("ccAlarmThreadPool")
    public void sendSimpleMailTraceId(String to,String subject,String content,String traceId){
        MDC.put("traceId", traceId);
        message = new SimpleMailMessage();
        message.setTo(to);//收信人
        message.setSubject(subject);//主题
        message.setText(content);//内容
        message.setFrom(from);//发信人
        try {
            mailSender.send(message);
        }catch (Exception e){
            System.out.println(e);
        }
    }
}
